package gw.com.android.ui.views;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.gwtsz.gts2.hx.R;

import www.com.library.view.NumberInput2;

/**
 * 各类价格数字调整控件，带左右按钮
 *
 * @author Administrator
 */
public class NumericEdit2 extends RelativeLayout {

    private boolean mRepeatBtnFired = true;
    private ImageView mBtnMinus = null;
    private ImageView mBtnPlus = null;
    private NumberInput2 mInputEdit = null; //价格输入框
    private OnOperationListener operationListener;

    private final OnClickListener mBtnClickListenerInternal = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == mBtnPlus) {
                mInputEdit.incMinSteps();
                if (operationListener != null) {
                    operationListener.onPlus();
                }
            } else if (v == mBtnMinus) {
                mInputEdit.decMinSteps();
                if (operationListener != null) {
                    operationListener.onMinus();
                }
            }
            mInputEdit.requestFocus();
            mInputEdit.setFocusable(true);
            mInputEdit.setSelection(mInputEdit.getText().length());
        }
    };

    public NumericEdit2(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public NumericEdit2(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public NumericEdit2(Context context) {
        super(context);
        init(context);
    }

    private void init(Context ctx) {
        setGravity(Gravity.CENTER);
        LayoutInflater inflater = LayoutInflater.from(ctx);
        View view = inflater.inflate(R.layout.order_numeric_edit2, this, true);

        mBtnMinus = (ImageView) view.findViewById(R.id.input_left_btn);
        mBtnPlus = (ImageView) view.findViewById(R.id.input_right_btn);
        mInputEdit = (NumberInput2) view.findViewById(R.id.number_input);

        //mBtnMinus.setOnClickListener(mBtnClickListenerInternal);
        //mBtnPlus.setOnClickListener(mBtnClickListenerInternal);

        if (mRepeatBtnFired) {
            mBtnMinus.setOnTouchListener(new OnTouchHoldListener(mBtnClickListenerInternal));
            mBtnPlus.setOnTouchListener(new OnTouchHoldListener(mBtnClickListenerInternal));
        }

        mInputEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mInputEdit.isMinValue()) {
                    mBtnMinus.setColorFilter(0xFFDEDEDE);
                } else {
                    mBtnMinus.setColorFilter(null);
                }
                if (mInputEdit.isMaxValue()) {
                    mBtnPlus.setColorFilter(0xFFDEDEDE);
                } else {
                    mBtnPlus.setColorFilter(null);
                }
            }
        });
    }

    public void setButtonRepeatFired(boolean repeated) {
        mRepeatBtnFired = repeated;
    }

    public View getFastIncButton() {
        return mBtnPlus;
    }

    public View getFineDecButton() {
        return mBtnMinus;
    }

    public NumberInput2 getNumericInput() {
        return mInputEdit;
    }

    public void setText(String text) {
        mInputEdit.setTextValue(text);
    }

    /**
     * 根据小数位返回是否为0
     */
    public boolean isEmptyOrZero(int digit) {
        return mInputEdit.isEmptyOrZero(digit);
    }

    /**
     * 止损止盈选项，判断输入框是否输入空值
     */
    public boolean isEmpty() {
        String value = getText();
        return value.equals("");
    }

    /**
     * 设置后缀值
     */
    public void setSuffixStr(String str) {
        mInputEdit.setSuffixStr(str);
    }

    /**
     * 设置默认值
     */
    public void setDefalutValue(String value) {
        mInputEdit.setDefalutValue(value);
    }

    /**
     * 返回数值
     */
    public double getValue() {
        return mInputEdit.getValue();
    }

    /**
     * 返回数值
     */
    public float getFloatValue() {
        return mInputEdit.getFloatValue();
    }

    /**
     * 返回数值
     */
    public int getIntValue() {
        return mInputEdit.getIntValue();
    }

    /**
     * 返回输入框值
     */
    public String getValueString() {
        return mInputEdit.getValueString();
    }

    public String getText() {
        return mInputEdit.getStrValue();
    }

    public void disabledWithText(String text) {
        setEnabled(false);
        mInputEdit.disabledWithText(text);
    }

    /**
     * 设置输入框内输入数字的范围
     */
    public void setDigits(int maxlength, int beforeDot, int afterDot) {
        mInputEdit.setDigits(maxlength, beforeDot, afterDot);
    }

    public void setDigits(String vaule) {
        if (vaule.contains(".")) {
            setDigits(vaule.length(), vaule.indexOf("."), vaule.length() - vaule.indexOf(".") - 1);
        } else {
            setDigits(vaule.length(), vaule.length(), 0);
        }
    }

    public void setDecimalDigits(int digits) {
        mInputEdit.setDecimalDigits(digits);
    }

    public void addTextChangedListener(TextWatcher watcher) {
        mInputEdit.addTextChangedListener(watcher);
    }

    public void setOnTextFocusChangeListener(OnFocusChangeListener l) {
        mInputEdit.setOnFocusChangeListener(l);
    }

    public void setMin(double min) {
        mInputEdit.setMin(min);
    }

    public void setMax(double max) {
        mInputEdit.setMax(max);
    }

    public void setMultipleSteps(int multiple) {
        mInputEdit.setMultipleSteps(multiple);
    }

    public void setMinSteps(double step) {
        mInputEdit.setMinSteps(step);
    }

    public void setHint(CharSequence hint) {
        mInputEdit.setHint(hint);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (enabled) {
            mInputEdit.setTextColor(getResources().getColor(R.color.color_b));
            setBackgroundResource(R.drawable.shape_gray_stroke);
            mBtnMinus.setColorFilter(null);
            mBtnPlus.setColorFilter(null);
        } else {
            setBackgroundResource(R.drawable.shape_gray_solid);
            mInputEdit.setTextColor(0xFFC7C7C7);
            mBtnMinus.setColorFilter(0xFFDEDEDE);
            mBtnPlus.setColorFilter(0xFFDEDEDE);
        }
    }

    public void setOnOperationListener(OnOperationListener listener) {
        this.operationListener = listener;
    }

    public interface OnOperationListener {

        void onMinus();

        void onPlus();

    }

}
